package com.eyeem.recyclerviewtools.sample.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eyeem.recyclerviewtools.sample.R;
import com.eyeem.recyclerviewtools.sample.data.Data;
import com.eyeem.recyclerviewtools.sample.gestureview.interface_.OnPhotoListener;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by budius on 23.03.16.
 */
public class DataNestAdapter extends ListAdapter<Data, DataNestAdapter.BaseHolder> implements View.OnClickListener {

    //public static final int SIZE = 260;
    public static final int SIZE = 20;
    private final OnPhotoListener listener;

    public static DataNestAdapter generateRandom(Context context) {
        ArrayList<Data> data = new ArrayList<>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            data.add(Data.newRandom(context));
        }
        return new DataNestAdapter(data, null);
    }

    public static DataNestAdapter generateRandom(Context context, OnPhotoListener listener) {
        ArrayList<Data> data = new ArrayList<>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            data.add(Data.newRandom(context));
        }
        return new DataNestAdapter(data, listener);
    }

    private LayoutInflater inflater;

    public DataNestAdapter(@NonNull ArrayList<Data> data, OnPhotoListener listener) {
        super(data);
        this.listener = listener;
        setHasStableIds(true);
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).type;
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (inflater == null) {
            inflater = LayoutInflater.from(parent.getContext());
        }
        return (viewType == Data.TYPE_PHOTO) ?
                new ImageHolder(inflater.inflate(R.layout.item_photo_nested, parent, false)).setImageClick(this) :
                new TextHolder(inflater.inflate(R.layout.item_text, parent, false));
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).hashCode();
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        holder.bind(getItem(position));
    }

    public static ImageView getImage(RecyclerView.ViewHolder holder) {
        if (holder instanceof ImageHolder) {
            return ((ImageHolder) holder).photo;
        } else {
            return null;
        }
    }

    @Override
    public void onClick(@NonNull View view) {
        //Data photo = (Data) view.getTag(view.getId());
        Data photo = (Data) view.getTag();
        int pos = data.indexOf(photo);
        if (listener != null) {
            listener.onPhotoClick(pos);
            listener.onPhotoClick(pos, view);
        }
    }

    public static abstract class BaseHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.profile)
        protected CircleImageView profile;
        @Bind(R.id.name)
        protected TextView name;

        public BaseHolder(View itemView) {
            super(itemView);
        }

        protected void bind(Data data) {
            Picasso.with(itemView.getContext())
                    .load(data.profileUrl)
                    //.resize(data.size, data.size)
                    .noFade()
                    .placeholder(placeholder(data.profileUrl))
                    .into(profile);
            name.setText(data.name);
        }
    }

    @SuppressWarnings({"unchecked", "Range"})
    public static Drawable placeholder(String url) {
        Drawable d = new ColorDrawable(Color.GRAY);
        int alpha = (url == null) ? 73 : 64 + Math.abs(url.hashCode()) % 64;
        d.setAlpha(alpha);
        return d;
    }

    static class ImageHolder extends BaseHolder {

        @Bind(R.id.photo)
        ImageView photo;
        final int size;
        @Bind(R.id.testRecycler)
        RecyclerView testRecycler;

        public ImageHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            size = itemView.getContext().getResources().getDisplayMetrics().widthPixels;
        }

        public ImageHolder setImageClick(View.OnClickListener clickListener) {
            photo.setOnClickListener(clickListener);
            return this;
        }

        @Override
        protected void bind(Data data) {
            super.bind(data);
            //photo.setTag(photo.getId(), data);
            photo.setTag(data);
            Picasso.with(itemView.getContext())
                    .load(data.photoUrl.replace("/h/100", "/h/" + size))
                    .fit()
                    .centerCrop()
                    .placeholder(placeholder(data.photoUrl))
                    .into(photo);
            LinearLayoutManager layoutManager = new LinearLayoutManager(testRecycler.getContext());
            testRecycler.setLayoutManager(layoutManager);
            testRecycler.setAdapter(new NestAdapter());
        }
    }


    static class TextHolder extends BaseHolder {

        @Bind(R.id.text)
        TextView text;

        public TextHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void bind(Data data) {
            super.bind(data);
            text.setText(data.text);
        }
    }


    //
    static class NestAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, null);
            return new NestHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            NestHolder holder1 = (NestHolder) holder;
            holder1.setText("text " + position);
        }

        @Override
        public int getItemCount() {
            return 10;
        }
    }

    static class NestHolder extends RecyclerView.ViewHolder {

        TextView textView;

        public NestHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(android.R.id.text1);
        }

        public void setText(String text) {
            textView.setText(text);
        }
    }


}
